package com.ymatou.iapi.godmch.testcase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oracle.net.aso.a;

import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.godmch.parameter.adminBindAgentBean;
import com.ymatou.iapi.godmch.parameter.applyAgentBean;
import com.ymatou.iapi.godmch.parameter.approveAgentBean;
import com.ymatou.iapi.godmch.parameter.createIndepMchBean;
import com.ymatou.iapi.godmch.parameter.createagentBean;
import com.ymatou.iapi.godmch.service.adminBindAgentCall;
import com.ymatou.iapi.godmch.service.applyAgentCall;
import com.ymatou.iapi.godmch.service.approveAgentCall;
import com.ymatou.iapi.godmch.service.createIndepMchCall;
import com.ymatou.iapi.godmch.service.createagentCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.agent_type;
import com.ymttest.database.model.mch_info;
import com.ymttest.database.model.mch_relation;
import com.ymttest.database.sqlwapper.godmch.agent_typeWapper;
import com.ymttest.database.sqlwapper.godmch.country_infoWapper;
import com.ymttest.database.sqlwapper.godmch.mch_infoWapper;
import com.ymttest.database.sqlwapper.godmch.mch_relationWapper;
/**
*代理审核通过
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_approveAgent{
	private static approveAgentBean bean;
	private static approveAgentCall call;
	
    agent_typeWapper agentTypeWapper=new agent_typeWapper();
	mch_infoWapper infoWapper=new mch_infoWapper();
	country_infoWapper country_infoWapper=new country_infoWapper();
	mch_relationWapper relationWapper=new mch_relationWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("代理审核通过");
	}

	@Before
	public void caseUp() {
		bean=new approveAgentBean();
		call=new approveAgentCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private void  check_verify(applyAgentBean applyBean,applyAgentCall applyCall) throws InterruptedException, JSONException {
		Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
		//mch_relation
		mch_relation relation=relationWapper.selectByPrimaryKey(applyCall.getAgentId(), applyBean.getMchId()).get(0);
		Logger.verifyEquals(bean.getAgentType(), relation.getAgentType(), "验证AgentType");
		Logger.verifyEquals(bean.getAgentMode(), relation.getAgentModel(), "验证AgentModel");
		Logger.verifyEquals(0, relation.getState(), "验证state");
		//mch_info
		List<Integer> mchIds=new ArrayList<Integer>();
		mchIds.add(applyCall.getAgentId());
		mch_info  info=infoWapper.selectbyMchIds(mchIds).get(0);
		Logger.verifyEquals(0, info.getState(), "验证state");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_approveAgent_001() {
		Logger.start(true,"正例-代理审核通过-默认");
		try {
			
			applyAgentBean applyBean=GodmchManageCallService.InitApplyAgentBean();
			applyAgentCall applyCall=GodmchManageCallService.ApplyAgentCall(applyBean);
			
			createagentBean agentBean=GodmchManageCallService.InitCreateAgentTypeBean(applyBean.getMchId());
			createagentCall agentCall=GodmchManageCallService.CreateAgentTypeCall(agentBean);
			//查询代理类型
			Map<String,Object> map=new HashMap();
			map.put("mchId", applyBean.getMchId());
			List<agent_type> list=agentTypeWapper.selectby(map);
			
			bean.setMchId(applyBean.getMchId());
			bean.setAgentId(applyCall.getAgentId());
			bean.setAgentType(list.get(0).getAgentType());
			bean.setAgentMode(1);//代理模式 1自主选货，2.自动继承
			
			call.setData(bean);
			call.callService();			

			check_verify(applyBean,applyCall);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_approveAgent_003() {
		Logger.start(true,"正例-代理审核通过-代理模式2自动继承");
		try {
			
			applyAgentBean applyBean=GodmchManageCallService.InitApplyAgentBean();
			applyAgentCall applyCall=GodmchManageCallService.ApplyAgentCall(applyBean);
			
			createagentBean agentBean=GodmchManageCallService.InitCreateAgentTypeBean(applyBean.getMchId());
			createagentCall agentCall=GodmchManageCallService.CreateAgentTypeCall(agentBean);
			//查询代理类型
			Map<String,Object> map=new HashMap();
			map.put("mchId", applyBean.getMchId());
			List<agent_type> list=agentTypeWapper.selectby(map);
			
			bean.setMchId(applyBean.getMchId());
			bean.setAgentId(applyCall.getAgentId());
			bean.setAgentType(list.get(0).getAgentType());
			bean.setAgentMode(2);//代理模式 1自主选货，2.自动继承
			
			call.setData(bean);
			call.callService();			

			check_verify(applyBean,applyCall);			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_approveAgent_004() {
		Logger.start(true,"正例-代理审核通过-mchId不存在");
		try {
			
			applyAgentBean applyBean=GodmchManageCallService.InitApplyAgentBean();
			applyAgentCall applyCall=GodmchManageCallService.ApplyAgentCall(applyBean);
			
			createagentBean agentBean=GodmchManageCallService.InitCreateAgentTypeBean(applyBean.getMchId());
			createagentCall agentCall=GodmchManageCallService.CreateAgentTypeCall(agentBean);
			//查询代理类型
			Map<String,Object> map=new HashMap();
			map.put("mchId", applyBean.getMchId());
			List<agent_type> list=agentTypeWapper.selectby(map);
			
			bean.setMchId(121212);
			bean.setAgentId(applyCall.getAgentId());
			bean.setAgentType(list.get(0).getAgentType());
			bean.setAgentMode(1);//代理模式 1自主选货，2.自动继承
			
			call.setData(bean);
			call.callService();					
			
			Logger.verifyEquals("5017", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("上级商户不存在"), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_approveAgent_005() {
		Logger.start(true,"正例-代理审核通过-mchId为空");
		try {
			
			applyAgentBean applyBean=GodmchManageCallService.InitApplyAgentBean();
			applyAgentCall applyCall=GodmchManageCallService.ApplyAgentCall(applyBean);
			
			createagentBean agentBean=GodmchManageCallService.InitCreateAgentTypeBean(applyBean.getMchId());
			createagentCall agentCall=GodmchManageCallService.CreateAgentTypeCall(agentBean);
			//查询代理类型
			Map<String,Object> map=new HashMap();
			map.put("mchId", applyBean.getMchId());
			List<agent_type> list=agentTypeWapper.selectby(map);
			
			bean.setMchId(null);
			bean.setAgentId(applyCall.getAgentId());
			bean.setAgentType(list.get(0).getAgentType());
			bean.setAgentMode(1);//代理模式 1自主选货，2.自动继承
			
			call.setData(bean);
			call.callService();	
			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");

			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_approveAgent_006() {
		Logger.start(true,"正例-代理审核通过-agentId代理商id不存在");
		try {

			applyAgentBean applyBean=GodmchManageCallService.InitApplyAgentBean();
			applyAgentCall applyCall=GodmchManageCallService.ApplyAgentCall(applyBean);
			
			createagentBean agentBean=GodmchManageCallService.InitCreateAgentTypeBean(applyBean.getMchId());
			createagentCall agentCall=GodmchManageCallService.CreateAgentTypeCall(agentBean);
			
			//查询代理类型
			Map<String,Object> map=new HashMap();
			map.put("mchId", applyBean.getMchId());
			List<agent_type> list=agentTypeWapper.selectby(map);
			
			bean.setMchId(applyBean.getMchId());
			bean.setAgentId(99999);
			bean.setAgentType(list.get(0).getAgentType());
			bean.setAgentMode(1);//代理模式 1自主选货，2.自动继承
			
			call.setData(bean);
			call.callService();	
					
			Logger.verifyEquals("5002", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("代理商不存在"), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_approveAgent_007() {
		Logger.start(true,"正例-代理审核通过-agentId代理商id为空");
		try {

			applyAgentBean applyBean=GodmchManageCallService.InitApplyAgentBean();
			applyAgentCall applyCall=GodmchManageCallService.ApplyAgentCall(applyBean);
			
			createagentBean agentBean=GodmchManageCallService.InitCreateAgentTypeBean(applyBean.getMchId());
			createagentCall agentCall=GodmchManageCallService.CreateAgentTypeCall(agentBean);
			
			//查询代理类型
			Map<String,Object> map=new HashMap();
			map.put("mchId", applyBean.getMchId());
			List<agent_type> list=agentTypeWapper.selectby(map);
			
			bean.setMchId(applyBean.getMchId());
			bean.setAgentId(null);
			bean.setAgentType(list.get(0).getAgentType());
			bean.setAgentMode(1);//代理模式 1自主选货，2.自动继承
			
			call.setData(bean);
			call.callService();	
					
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_approveAgent_008() {
		Logger.start(true,"正例-代理审核通过-agentType不存在");
		try {

			applyAgentBean applyBean=GodmchManageCallService.InitApplyAgentBean();
			applyAgentCall applyCall=GodmchManageCallService.ApplyAgentCall(applyBean);
			
			createagentBean agentBean=GodmchManageCallService.InitCreateAgentTypeBean(applyBean.getMchId());
			createagentCall agentCall=GodmchManageCallService.CreateAgentTypeCall(agentBean);
			
			bean.setMchId(applyBean.getMchId());
			bean.setAgentId(applyCall.getAgentId());
			bean.setAgentType(99999);
			bean.setAgentMode(1);//代理模式 1自主选货，2.自动继承
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("5001", call.getStatusCode1(), "验证status");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_approveAgent_009() {
		Logger.start(true,"正例-代理审核通过-agentType为空");
		try {

			applyAgentBean applyBean=GodmchManageCallService.InitApplyAgentBean();
			applyAgentCall applyCall=GodmchManageCallService.ApplyAgentCall(applyBean);
			
			createagentBean agentBean=GodmchManageCallService.InitCreateAgentTypeBean(applyBean.getMchId());
			createagentCall agentCall=GodmchManageCallService.CreateAgentTypeCall(agentBean);
			
			bean.setMchId(applyBean.getMchId());
			bean.setAgentId(applyCall.getAgentId());
			bean.setAgentType(null);
			bean.setAgentMode(1);//代理模式 1自主选货，2.自动继承
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_approveAgent_010() {
		Logger.start(true,"正例-申请成为代理商-申请通过-解绑,mch_info status=0,关系表 status=-1(曾经通过后，不管是怎么操作，商户表一直是0 不变，改变关系表)");
		try {
			
			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);		
			//申请代理
			applyAgentBean applyBean=GodmchManageCallService.InitApplyAgentBean();
			applyBean.setMchId(createCall.getmchId());
			applyAgentCall applyCall=GodmchManageCallService.ApplyAgentCall(applyBean);

			//查询代理类型
			Map<String,Object> map=new HashMap();
			map.put("mchId", applyBean.getMchId());
			List<agent_type> list=agentTypeWapper.selectby(map);
			//审核通过
			approveAgentBean approveBean=GodmchManageCallService.InitApproveAgentBean();
			approveBean.setMchId(applyBean.getMchId());
			approveBean.setAgentId(applyCall.getAgentId());
			approveBean.setAgentType(list.get(0).getAgentType());
			GodmchManageCallService.ApproveAgentCall(approveBean);
			//解绑
			GodmchManageCallService.InitremovemchForAgent(applyBean.getMchId(),applyCall.getAgentId());
			
			//验证
		    Logger.verifyEquals("200", applyCall.getStatusCode1(), "验证status");
			Logger.verifyEquals("请求成功", applyCall.getMessage(), "验证Message");
			List<Integer> mchIds=new ArrayList<Integer>();
			mchIds.add(applyCall.getAgentId());
			mch_info  info=infoWapper.selectbyMchIds(mchIds).get(0);
			
			Logger.verifyEquals(true, info.getMchId()>0, "验证数据库mch_info新增一条数据");
			//mch_info
			Logger.verifyEquals(1, info.getAgent(), "验证数据库mch_info中的agent");
			Logger.verifyEquals(1, info.getChannels(), "验证数据库mch_info中的Channels");
			Logger.verifyEquals(0, info.getState(), "验证数据库mch_info中的State");//曾经通过后，不管是怎么操作，商户表一直是0 不变，改变关系表
			
			//mch_relation
			mch_relation relation=relationWapper.selectByPrimaryKey(applyCall.getAgentId(), applyBean.getMchId()).get(0);
			Logger.verifyEquals(-1, relation.getState(), "验证state");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_approveAgent_011() {
		Logger.start(true,"正例-代理审核通过-运营绑定的代理 自主选货-返回成功");
		try {
			
			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			//代理商户
			createIndepMchBean createBean2=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall2=GodmchManageCallService.CreateIndepMchCall(createBean2);
			//运营绑定的代理
			adminBindAgentBean adminBindAgentBean=GodmchManageCallService.initAdminBindAgent(createCall.getmchId(),createCall2.getmchId());
			adminBindAgentCall adminBindAgentCall=GodmchManageCallService.AdminBindAgent(adminBindAgentBean);
			
			//查询代理类型
			Map<String,Object> map=new HashMap();
			map.put("mchId", adminBindAgentBean.getMchId());
			List<agent_type> list=agentTypeWapper.selectby(map);
			
			bean.setMchId(adminBindAgentBean.getMchId());
			bean.setAgentId(adminBindAgentBean.getAgentId());
			bean.setAgentType(list.get(0).getAgentType());
			bean.setAgentMode(1);//代理模式 1自主选货，2.自动继承
			
			call.setData(bean);
			call.callService();			

			//验证
		    Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals("请求成功", call.getMessage(), "验证Message");
			//mch_relation
			mch_relation relation=relationWapper.selectByPrimaryKey(adminBindAgentBean.getAgentId(), adminBindAgentBean.getMchId()).get(0);
			Logger.verifyEquals(bean.getAgentType(), relation.getAgentType(), "验证AgentType");
			Logger.verifyEquals(bean.getAgentMode(), relation.getAgentModel(), "验证AgentModel");
			Logger.verifyEquals(0, relation.getState(), "验证state");
			//mch_info
			List<Integer> mchIds=new ArrayList<Integer>();
			mchIds.add(adminBindAgentBean.getAgentId());
			mch_info  info=infoWapper.selectbyMchIds(mchIds).get(0);
			Logger.verifyEquals(0, info.getState(), "验证state");
			Logger.verifyEquals(1, info.getAgent(), "验证Agent");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_approveAgent_012() {
		Logger.start(true,"正例-代理审核通过-运营绑定的代理 自动继承-返回失败");
		try {
			
			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			//代理商户
			createIndepMchBean createBean2=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall2=GodmchManageCallService.CreateIndepMchCall(createBean2);
			//运营绑定的代理
			adminBindAgentBean adminBindAgentBean=GodmchManageCallService.initAdminBindAgent(createCall.getmchId(),createCall2.getmchId());
			adminBindAgentCall adminBindAgentCall=GodmchManageCallService.AdminBindAgent(adminBindAgentBean);
			
			//查询代理类型
			Map<String,Object> map=new HashMap();
			map.put("mchId", adminBindAgentBean.getMchId());
			List<agent_type> list=agentTypeWapper.selectby(map);
			
			bean.setMchId(adminBindAgentBean.getMchId());
			bean.setAgentId(adminBindAgentBean.getAgentId());
			bean.setAgentType(list.get(0).getAgentType());
			bean.setAgentMode(2);//代理模式 1自主选货，2.自动继承
			
			call.setData(bean);
			call.callService();			

			//验证
		    Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("跨平台代理商不支持使用自动继承型模式"), "验证Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
